package com.mqtt.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mqtt.common.utils.SecurityUtils;
import com.mqtt.system.domain.Device;
import com.mqtt.system.domain.Nodes;
import com.mqtt.system.domain.vo.ExResult;
import com.mqtt.system.mapper.DeviceMapper;
import com.mqtt.system.mapper.NodesMapper;
import com.mqtt.system.service.DeviceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wang
 * @version 1.0
 * @data 2024/5/9 下午 10:34
 * @desc 描述信息
 */
@Service
public class DeviceServiceImpl implements DeviceService {
    @Resource
    private DeviceMapper deviceMapper;
    @Resource
    private NodesMapper nodesMapper;
    private ExResult exResult = new ExResult();

    @Override
    public List<Nodes> nodeList() {
        Long userId = SecurityUtils.getUserId();

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        List<Nodes> list = nodesMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public ExResult createNode(Nodes nodes) {
        Long userId = SecurityUtils.getUserId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("node_name", nodes.getNodeName());
        List list = nodesMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            exResult.setResult(false);
            exResult.setMsg("已经存在相同节点");
            return exResult;
        }
        int insert = nodesMapper.insert(nodes);
        if (insert > 0) {
            exResult.setResult(true);
            exResult.setMsg("添加成功");
        } else {
            exResult.setResult(false);
            exResult.setMsg("添加失败");
        }
        return exResult;
    }

    @Override
    public Boolean nodeDelete(Long id) {

        return nodesMapper.deleteById(id) > 0;
    }

    @Override
    public ExResult nodeUpdate(Nodes nodes) {
        Long userId = SecurityUtils.getUserId();

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("node_name", nodes.getNodeName());
        List list = nodesMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            exResult.setResult(false);
            exResult.setMsg("已经存在相同节点");
            return exResult;
        }

        int insert = nodesMapper.update(nodes, null);
        if (insert > 0) {
            exResult.setResult(true);
            exResult.setMsg("修改成功");
        } else {
            exResult.setResult(false);
            exResult.setMsg("修改失败");
        }
        return exResult;
    }

    @Override
    public List<Device> deviceList(Long nodeId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("node_id", nodeId);
        List<Device> list = deviceMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public ExResult deviceCreate(Device device) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("device_name", device.getDeviceName());
        List list = deviceMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            exResult.setResult(false);
            exResult.setMsg("已经存在相同设备");
            return exResult;
        }
        int insert = deviceMapper.insert(device);
        if (insert > 0) {
            exResult.setResult(true);
            exResult.setMsg("创建成功");
        } else {
            exResult.setResult(false);
            exResult.setMsg("创建失败");
        }
        return exResult;
    }

    @Override
    public Boolean deviceDelete(Long id) {
        int insert = deviceMapper.deleteById(id);

        return insert > 0;
    }

    @Override
    public ExResult deviceUpdate(Device device) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("device_name", device.getDeviceName());
        List list = deviceMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            exResult.setResult(false);
            exResult.setMsg("已经存在相同设备");
            return exResult;
        }
        int insert = deviceMapper.update(device,null);
        if (insert > 0) {
            exResult.setResult(true);
            exResult.setMsg("更新成功");
        } else {
            exResult.setResult(false);
            exResult.setMsg("更新失败");
        }
        return exResult;
    }
}
